Imports CacheMyWork.UserConfiguration

Public Class uiHideApplications

#Region " Variables "

    '' Don't know if any of these are at all necessary
    ''
    'Friend AllHiddenApplications As Collection ' To pass the consolidated list of apps [as ProcessName strings] to be removed from the Applications List in the main UI
    'Friend ReEnabledApplications As Collection ' To pass the list of apps [as ProcessName strings] to be added back to (re-enabled in) the Applications List in the main UI
    'Dim CurrentHiddenApplications As Collection ' To cache the list of apps passed into this form's UI, so that it can be used as a comparison after the user submits their changes
    'Dim UserConfig As CacheMyWork.UserConfiguration

#End Region

    #Region " Event Handlers "

    Private Sub uiHideApplicationsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Set some properties of the checklistbox'es for their eventual display
        uiCurrentAppsList.CheckOnClick = True
        uiCurrentAppsList.Sorted = True
        uiAlreadyHiddenAppsList.CheckOnClick = True
        uiAlreadyHiddenAppsList.Sorted = True

        PopulateEnabledAndHiddenApplicationLists()

    End Sub

    Private Sub uiHideApplications_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    End Sub

    Private Sub uiOKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiOKButton.Click

        Dim IsHiddenAppsChanged As Boolean ' This will track whether any changes are made by the user

        ' This will collect the apps that the user has selected to be "hidden" from the main checklist [i.e. apps they don't even want to distract them in the list]
        ' Any checkbox that the user has toggled "on" (aka "checked") is an app that the user wants to hide
        ' Any checkbox that the user has toggled "off" (aka "cleared") is an app that the user no longer wants to hide from their "apps to be cached" list

        ' TODO: (v2) convert this to a side-by-side set of lists, where you can move a listing from the "Enabled" list to the "Hidden" list


        ' Review each checked item and ensure that it is updated to the main UI and persisted to user settings
        ' TODO: determine how to convert the Value retrieved from this checkedlistbox into the processapplication object that will be used to make adjustments using the NewlyHiddenApplications collection
        ' TODO: if this is the same ProcessesToShowUser collection used in both contexts, then figure out how to carry the list of references to the newly hidden apps into the context where I'll act on this "diff" list.
        '       TODO: Is this ^ question really just a matter of getting a standard "key" to the collection (e.g. ProcessName), and using that as the Value in all cases where I'm using DataBinding?
        For Each _Item As String In uiCurrentAppsList.CheckedItems
            ' Derive the ProcessName for the checked item, and add the ProcessName string to the UserConfiguration.HiddenApps collection
            ' TODO: !!! use the ArrayList that is this control's DataSource to derive the ProcessName

            ' Derive the index value in UserProcesses collection for the checked item
            ' TODO: !! set the .IsHiddenByUser property to True for the _Item in the uiMainForm.UserProcesses collection

            ' Indicate that the user made a change
            IsHiddenAppsChanged = True
        Next

        ' Any entry in uiAlreadyHiddenAppsList from which the user removed the checkmark is considered a re-enabled application
        ' The re-enabled applications are no longer hidden, and should be removed from the hidden apps configured list as well as re-displayed in the MainForm UI
        ' TODO: determine if there's any advantage to this i=1 to .Count approach over For Each in uiAlreadyHiddenAppsList.CheckedItems
        For Each _Item As String In uiAlreadyHiddenAppsList.Items
            If uiAlreadyHiddenAppsList.GetItemChecked(_Item) = False Then
                ' Derive the ProcessName for the UNchecked item, and remove the ProcessName string from the UserConfiguration.HiddenApps collection
                ' TODO: !!! use the ArrayList that is this control's DataSource to derive the ProcessName

                ' Derive the index value in UserProcesses collection for the checked item
                ' TODO: !! set the .IsHiddenByUser property to False for the _Item in the uiMainForm.UserProcesses collection

                ' Indicate that the user made a change
                IsHiddenAppsChanged = True
            End If
        Next

        ' persist the user's application settings
        SetAppConfig()

        ' Remove any newly-hidden apps from MainForm.uiApplicationsList, and add back any newly-removed apps
        If IsHiddenAppsChanged <> False Then
            ' Throw a new Event that causes uiMainForm to refresh call PopulateChecklistWithNonHiddenApps()
            ' TODO: !! Create a new Event that is thrown here, and be sure to create the Event Handler in uiMainForm

        End If

        Me.Close()

    End Sub

    #End Region

#Region " Private Methods "

    Private Sub PopulateEnabledAndHiddenApplicationLists()
        ' This function will enumerate all processes that would be/have been shown to the user, and removes those that the user has previously
        '    configured to be hidden from the display list.

        ' TODO: ensure each entry's displayed name is unique in some way. (e.g. append the PID)
        For Each _Process As CacheMyWork.EnumeratedProcess In CacheMyWork.ProcessesBusinessLogic.EnumeratedProcesses.Values

            ' First populate the ActiveAppsList - only those apps not hidden by the user or the code
            If (_Process.ProcessWillBeDisplayedToUser) Then

                Try
                    ' Add the non-hidden processes to the ActiveAppsList CheckedListBox in the Form:
                    ' TODO: !!! Create a class-wide ArrayList that will be used as the DataSource for the CheckedListBox, and index it by ProcessName (there are no duplicate entries)
                    uiCurrentAppsList.Items.Add(_Process.ProcessDescriptiveName)

                Catch ex As System.ApplicationException
                    ' The only known case where this Exception occurs is when the process exits after it's added to the ProcessesToShowUser array
                    ' Since we're tracking this state in the EnumeratedProcesses class, we shall toggle the .Exists property to "False"
                    _Process.Exists = False

                End Try

                ' Whether .IsRemovedProgrammatically is true or false, the user intentionally set this app to "Hidden", so we'll display it in the AlreadyHiddenApps list
            ElseIf (_Process.IsHiddenByUser) Then

                ' Check whether the process exists at present
                If _Process.Exists = False Then

                    ' If not, append "(not currently running)" to the display value and add the hidden app to the AlreadyHiddenApps list
                    uiAlreadyHiddenAppsList.Items.Add(_Process.ProcessDescriptiveName + " (not currently running)")
                Else

                    ' Add the hidden app to the AlreadyHiddenApps list
                    uiAlreadyHiddenAppsList.Items.Add(_Process.ProcessDescriptiveName)

                    ' TODO: Finally, toggle uiAlreadyHiddenAppsList.SetItemChecked(_Item, True) for all Hidden apps, to signify to the end user that they're already being hidden
                End If

            End If

        Next _Process
    End Sub

#End Region

End Class